#include <iostream>
#include <map>

using namespace std;

class RangeModule {
public:
    RangeModule()
    {}

    void addRange(int left, int right) {
        std::map<int, int>::iterator it = intervals.upper_bound(left);
        if (it != intervals.begin())
        {
            std::map<int, int>::iterator start = prev(it);
            if (start->second >= right)
            {
                return;
            }

            if (start->second >= left)
            {
                left = start->first;
                intervals.erase(start);
            }
        }

        while (it != intervals.end() && it->first <= right)
        {
            right = max(right, it->second);
            it = intervals.erase(it);
        }
        intervals[left] = right;
    }

    bool queryRange(int left, int right) {
        std::map<int, int>::iterator it = intervals.upper_bound(left);
        if (it == intervals.begin())
        {
            return false;
        }

        it = prev(it);
        return right <= it->second;
    }

    void removeRange(int left, int right) {
        std::map<int, int>::iterator it = intervals.upper_bound(left);
        if (it != intervals.begin())
        {
            std::map<int, int>::iterator start = prev(it);
            if (start->second >= right)
            {
                int ri = start->second;
                if (start->first == left)
                {
                    intervals.erase(start);
                }
                else
                {
                    start->second = left;
                }
                if (right != ri)
                {
                    intervals[right] = ri;
                }
                return;
            }
            else if (start->second > left)
            {
                if (start->first == left)
                {
                    intervals.erase(start);
                }
                else
                {
                    start->second = left;
                }
            }
        }

        while (it != intervals.end() && it->first < right)
        {
            if (it->second <= right)
            {
                it = intervals.erase(it);
            }
            else
            {
                intervals[right] = it->second;
                intervals.erase(it);
                break;
            }
        }


    }

private:
    map<int, int> intervals;
};